﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace AbacusCustom
{
    public partial class ColumnList : Form
    {
        frmCreateReport frmcreatereport;
        String urutan;
        public ColumnList(frmCreateReport frmcreatereport,String urutan)
        {
            InitializeComponent();
            this.frmcreatereport = frmcreatereport;
            this.urutan = urutan;
        }

        private void ColumnList_Load(object sender, EventArgs e)
        {
            Module db_conf = new Module();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
            "Data Source=" + db_conf.db_host + ";" +
            "Database=PSCUSTOM;" +
            "User ID=" + db_conf.db_user + ";" +
            "Password=" + db_conf.db_password + ";";
            conn.Open();

            String tablename = "";
            if (urutan == "1")
            {
                tablename = Module.selectedtable1;
            }
            else if (urutan == "2")
            {
                tablename = Module.selectedtable2;
            }
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT t1.NAME FROM PS_COLUMN t1 JOIN PS_TABLE t2 ON t1.TABLE_ID = t2.ID WHERE t2.NAME = '"+tablename+"'";

            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                DataGridViewRow row2 = new DataGridViewRow();
                row2.CreateCells(dataGridView1);
                row2.Cells[0].Value = reader.GetValue(0).ToString();
                dataGridView1.Rows.Add(row2);
                
            }
            conn.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmcreatereport.setvalue(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim());
            if (Module.selectedcolumn == 0)
            {
                Module.selectedtable1 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
            }
            else if (Module.selectedcolumn == 3)
            {
                Module.selectedtable2 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
            }
            this.Close();
        }
    }
}
